import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Banner } from '../entities/banner.entity'
import { Repository } from 'typeorm'
import { Recommend } from '../entities/recommend.entity'
import { Goods } from '../entities/goods.entity'
import { Detail } from '../entities/detail.entity'

@Injectable()
export class HomeService {
  constructor(
    @InjectRepository(Banner) private readonly bannerRepository: Repository<Banner>,
    @InjectRepository(Recommend)
    private readonly recommendRepository: Repository<Recommend>,
    @InjectRepository(Goods)
    private readonly goodsRepository: Repository<Goods>,
    @InjectRepository(Detail)
    private readonly detailRepository: Repository<Detail>,
  ) {}

  /**
   * 查询轮播数据
   */
  async queryBanners() {
    return this.bannerRepository.find()
  }

  /**
   * 查询热门数据
   */
  queryRecommend() {
    return this.recommendRepository.find()
  }

  /**
   * 查询商品列表
   * @param type
   * @param page
   */
  async queryGoods({ type, page }) {
    // 计算 起始位置 30
    const skip = (page - 1) * 30
    return await this.goodsRepository.find({
      where: { type },
      take: 30,
      skip,
    })
  }

  /**
   * 查询商品的详情
   * @param iid
   */
  async queryGoodsDetail(iid: string) {
    return await this.detailRepository.findOne({ where: { iid } })
  }
}
